<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: Core.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_Plugin_Core extends Zend_Controller_Plugin_Abstract {
	
	public function routeShutdown(Zend_Controller_Request_Abstract $request) {
		if (substr ( $request->getPathInfo (), 1, 5 ) == "admin") {
			$module = $request->getModuleName ();
			$controller = $request->getControllerName ();
			$action = $request->getActionName ();
			if ($module == 'core' && $controller == 'admin-content' && $action == 'index') {
				$sitepageLayoutCreate = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.layoutcreate' );
				if (! empty ( $sitepageLayoutCreate )) {
					$page_id = Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'page', null );
					if (! empty ( $page_id )) {
						$corepageTable = Engine_Api::_ ()->getDbtable ( 'pages', 'core' );
						$corepageTableName = $corepageTable->info ( 'name' );
						$select = $corepageTable->select ()->from ( $corepageTableName )->where ( 'page_id' . ' = ?', $page_id )->where ( 'name' . ' = ?', 'sitepage_index_view' )->limit ( 1 );
						$corepageTableInfo = $corepageTable->fetchRow ( $select );
					}
					if (! empty ( $corepageTableInfo )) {
						$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper ( 'redirector' );
						$redirector->gotoRoute ( array ('module' => 'sitepage', 'controller' => 'layout', 'action' => 'layout', 'page' => $page_id ), 'admin_default', false );
					}
				}
			}
		}
		
		if (! Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'mobi' ))
			return;
		
		//CHECK IF ADMIN
		if (substr ( $request->getPathInfo (), 1, 5 ) == "admin") {
			return;
		}
		
		$mobile = $request->getParam ( "mobile" );
		$session = new Zend_Session_Namespace ( 'mobile' );
		
		if ($mobile == "1") {
			$mobile = true;
			$session->mobile = true;
		} elseif ($mobile == "0") {
			$mobile = false;
			$session->mobile = false;
		} else {
			if (isset ( $session->mobile )) {
				$mobile = $session->mobile;
			} else {
				//CHECK TO SEE IF MOBILE
				if (Engine_Api::_ ()->mobi ()->isMobile ()) {
					$mobile = true;
					$session->mobile = true;
				} else {
					$mobile = false;
					$session->mobile = false;
				}
			}
		}
		
		if (! $mobile) {
			return;
		}
		$module = $request->getModuleName ();
		$controller = $request->getControllerName ();
		$action = $request->getActionName ();
		if ($module == "sitepage") {
			if ($controller == "index" && $action == "home") {
				$request->setControllerName ( 'mobi' );
				$request->setActionName ( 'home' );
			}
			
			if ($controller == "index" && $action == "index") {
				$request->setControllerName ( 'mobi' );
				$request->setActionName ( 'index' );
			}
			
			if ($controller == "index" && $action == "view") {
				
				$request->setControllerName ( 'mobi' );
				$request->setActionName ( 'view' );
			}
		}
		
		//CREATE LAYOUT
		$layout = Zend_Layout::startMvc ();
		
		//SET OPTIONS
		$layout->setViewBasePath ( APPLICATION_PATH . "/application/modules/Mobi/layouts", 'Core_Layout_View' )->setViewSuffix ( 'tpl' )->setLayout ( null );
	}
	
	public function onRenderLayoutDefault($event) {
		
		$view = Zend_Registry::isRegistered ( 'Zend_View' ) ? Zend_Registry::get ( 'Zend_View' ) : null;
		$front = Zend_Controller_Front::getInstance ();
		$module = $front->getRequest ()->getModuleName ();
		$controller = $front->getRequest ()->getControllerName ();
		$action = $front->getRequest ()->getActionName ();
		
		$view->headLink ()->appendStylesheet ( $view->seaddonsBaseUrl () . '/application/modules/Sitepage/externals/styles/style_sitepage.css' );
		if ($module == "sitepage") {
			if ($controller == "index" && $action == "index") {
				$siteinfo = $view->layout ()->siteinfo;
				if (Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'categoryname', null )) {
					$siteinfo ['keywords'] = Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'categoryname', null );
				}
				if (Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'subcategoryname', null )) {
					$siteinfo ['keywords'] .= ',' . Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'subcategoryname', null );
				}
				
				if (! empty ( $_GET ['location'] )) {
					if (Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'location', null )) {
						$siteinfo ['keywords'] .= ',';
					}
					$siteinfo ['keywords'] .= $_GET ['location'];
				}
				$view->layout ()->siteinfo = $siteinfo;
			}
			
			$category_name = "";
			$subcategory_name = "";
			if (Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'page_id', null )) {
				$currentpageid = Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'page_id', null );
				$sitepageObject = Engine_Api::_ ()->getItem ( 'sitepage_page', $currentpageid );
				$siteinfo = $view->layout ()->siteinfo;
				$row = Engine_Api::_ ()->getDbTable ( 'categories', 'sitepage' )->getCategory ( $sitepageObject->category_id );
				if (! empty ( $row->category_name )) {
					$category_name = $row->category_name;
					$siteinfo ['keywords'] = $category_name;
				}
				$row = Engine_Api::_ ()->getDbTable ( 'categories', 'sitepage' )->getCategory ( $sitepageObject->subcategory_id );
				if (! empty ( $row->category_name )) {
					$subcategory_name = $row->category_name;
					$siteinfo ['keywords'] .= ',' . $subcategory_name;
				}
				
				if (! empty ( $sitepageObject->location )) {
					$siteinfo ['keywords'] .= ',' . $sitepageObject->location;
				}
				$view->layout ()->siteinfo = $siteinfo;
			}
			if ($controller == "index" && $action == "view") {
				if (Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'page_url', null )) {
					$currenttabid = Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'tab', 0 );
					$page_url = Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'page_url', null );
					$contentinformation = 0;
					$view = $event->getPayload ();
					$id = Engine_Api::_ ()->sitepage ()->getPageId ( $page_url );
					$sitepage_title = '';
					if (! empty ( $id )) {
						$sitepage = Engine_Api::_ ()->getItem ( 'sitepage_page', $id );
						$sitepage_title = Engine_Api::_ ()->sitepage ()->parseString ( $sitepage->title );
					}
					
					$content_id = 0;
					$content_id3 = 0;
					$tab_id = 0;
					$tab_id3 = 0;
					$widgetinformation = 0;
					$tempcontent_name = "";
					$tempcontent_id = "";
					$newtabid = "";
					if (Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.layoutcreate', 0 ) && ! empty ( $sitepage )) {
						$row = Engine_Api::_ ()->getDbtable ( 'contentpages', 'sitepage' )->getContentPageId ( $id );
						if ($row !== null) {
							$contentpage_id = $row->contentpage_id;
							$siteinfo = $view->layout ()->siteinfo;
							$siteinfo ['description'] = $row->description;
							$siteinfo ['keywords'] = $row->keywords;
							$view->layout ()->siteinfo = $siteinfo;
							$rowinfo = Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->getContentInformation ( $contentpage_id );
							
							if (! empty ( $rowinfo )) {
								foreach ( $rowinfo as $key => $value ) {
									if ($value->name == 'socialengineaddon.feed') {
										$content_id = $tab_id = $value->content_id;
										if (empty ( $currenttabid )) {
											$currenttabid = $content_id;
										}
									} elseif ($value->name == 'activity.feed') {
										$content_id = $tab_id = $value->content_id;
										if (empty ( $currenttabid )) {
											$currenttabid = $content_id;
										}
									} else if ($value->name == 'core.profile-links') {
										$content_id3 = $tab_id3 = $value->content_id;
										if (empty ( $currenttabid )) {
											$currenttabid = $content_id3;
										}
									}
								}
							}
						}
						
						if (! empty ( $contentpage_id )) {
							$contentinfo = Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->getContentByWidgetName ( 'core.container-tabs', $contentpage_id );
							if (empty ( $contentinfo )) {
								$contentinformation = 0;
							} else {
								$contentinformation = 1;
							}
							
							$contentwidgetinfo = Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->getContentByWidgetName ( 'sitepage.widgetlinks-sitepage', $contentpage_id );
							if (empty ( $contentwidgetinfo )) {
								$widgetinformation = 0;
							} else {
								$widgetinformation = 1;
							}
						}
						
						$default_content_id = Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->getContentId ( $contentpage_id, $sitepage );
						$tempcontent_name = $default_content_id ['content_name'];
						$tempcontent_id = $default_content_id ['content_id'];
						$newtabid = Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'tab', $tempcontent_id );
						$tab_main = Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'tab', null );
						if ($tab_main) {
							$tempcontent_name = Engine_Api::_ ()->getDbtable ( 'content', 'sitepage' )->getCurrentTabName ( $tab_main );
							$tempcontent_id = $tab_main;
						}
					} else {
						$table = Engine_Api::_ ()->getDbtable ( 'pages', 'core' );
						$select = $table->select ()->where ( 'name = ?', 'sitepage_index_view' )->limit ( 1 );
						$row = $table->fetchRow ( $select );
						if ($row !== null) {
							$page_id = $row->page_id;
							$table = Engine_Api::_ ()->getDbtable ( 'content', 'core' );
							$select = $table->select ()->where ( "name IN ('sitepage.info-sitepage', 'socialengineaddon.feed', 'activity.feed', 'sitepage.location-sitepage', 'core.profile-links')" )->where ( 'page_id = ?', $page_id );
							$rowinfo = $table->fetchAll ( $select );
							if (! empty ( $rowinfo )) {
								foreach ( $rowinfo as $key => $value ) {
									if ($value->name == 'socialengineaddon.feed') {
										$content_id = $tab_id = $value->content_id;
										if (empty ( $currenttabid )) {
											$currenttabid = $content_id;
										}
									} elseif ($value->name == 'activity.feed') {
										$content_id = $tab_id = $value->content_id;
										if (empty ( $currenttabid )) {
											$currenttabid = $content_id;
										}
									} else if ($value->name == 'core.profile-links') {
										$content_id3 = $tab_id3 = $value->content_id;
										if (empty ( $currenttabid )) {
											$currenttabid = $content_id3;
										}
									}
								}
							}
						}
						$table = Engine_Api::_ ()->getDbtable ( 'content', 'core' );
						$tablename = $table->info ( 'name' );
						if (! empty ( $page_id )) {
							$selectContent = $table->select ()->from ( $tablename )->where ( 'name =?', 'core.container-tabs' )->where ( 'page_id =?', $page_id )->limit ( 1 );
							$contentinfo = $selectContent->query ()->fetchAll ();
							if (empty ( $contentinfo )) {
								$contentinformation = 0;
							} else {
								$contentinformation = 1;
							}
						}
						
						if (! empty ( $page_id )) {
							$selectContent = $table->select ()->from ( $tablename )->where ( 'name =?', 'sitepage.widgetlinks-sitepage' )->where ( 'page_id =?', $page_id )->limit ( 1 );
							$contentwidgetinfo = $selectContent->query ()->fetchAll ();
							if (empty ( $contentwidgetinfo )) {
								$widgetinformation = 0;
							} else {
								$widgetinformation = 1;
							}
						}
						
						$selectPage = Engine_Api::_ ()->sitepage ()->getWidgetizedPage ();
						if (! empty ( $selectPage )) {
							$pageid = $selectPage->page_id;
							if (! empty ( $pageid )) {
								$tableCore = Engine_Api::_ ()->getDbtable ( 'content', 'core' );
								$select = $tableCore->select ();
								$select_content = $select->from ( $tableCore->info ( 'name' ) )->where ( 'page_id = ?', $pageid )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'main' )->limit ( 1 );
								$content = $select_content->query ()->fetchAll ();
								if (! empty ( $content )) {
									$select = $tableCore->select ();
									$select_container = $select->from ( $tableCore->info ( 'name' ), array ('content_id' ) )->where ( 'page_id = ?', $pageid )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->limit ( 1 );
									$container = $select_container->query ()->fetchAll ();
									if (! empty ( $container )) {
										$select = $tableCore->select ();
										$container_id = $container [0] ['content_id'];
										$select_middle = $select->from ( $tableCore->info ( 'name' ) )->where ( 'parent_content_id = ?', $container_id )->where ( 'type = ?', 'widget' )->where ( 'name = ?', 'core.container-tabs' )->where ( 'page_id = ?', $pageid )->limit ( 1 );
										$middle = $select_middle->query ()->fetchAll ();
										if (! empty ( $middle )) {
											$content_id = $middle [0] ['content_id'];
										} else {
											$content_id = $container_id;
										}
									}
								}
								
								if (! empty ( $content_id ) && ! empty ( $sitepage )) {
									$select = $tableCore->select ();
									$select_middle = $select->from ( $tableCore->info ( 'name' ), array ('content_id', 'name' ) )->where ( 'parent_content_id = ?', $content_id )->where ( 'type = ?', 'widget' )->where ( "name NOT IN ('sitepage.title-sitepage', 'sitepage.page-like-button', 'sitepage.photorecent-sitepage', 'Facebookse.facebookse-sitepageprofilelike', 'sitepage.thumbphoto-sitepage')" )->where ( 'page_id = ?', $pageid )->order ( 'order ASC' );
									
									$middle = $select_middle->query ()->fetchAll ();
									$flag = false;
									$editpermission = '';
									$isManageAdmin = '';
									foreach ( $middle as $value ) {
										$content_name = $value ['name'];
										switch ($content_name) {
											case 'sitepage.overview-sitepage' :
												if (! empty ( $sitepage )) {
													$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'overview' );
													if (! empty ( $isManageAdmin )) {
														$editpermission = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'edit' );
														if (! empty ( $editpermission ) && empty ( $sitepage->overview )) {
															$flag = true;
														} elseif (empty ( $editpermission ) && empty ( $sitepage->overview )) {
															$flag = false;
														} elseif (! empty ( $editpermission ) && ! empty ( $sitepage->overview )) {
															$flag = true;
														} elseif (empty ( $editpermission ) && ! empty ( $sitepage->overview )) {
															$flag = true;
														}
													}
												}
												break;
											case 'sitepage.location-sitepage' :
												if (! empty ( $sitepage )) {
													$isManageAdmin = Engine_Api::_ ()->sitepage ()->isManageAdmin ( $sitepage, 'map' );
													if (! empty ( $isManageAdmin )) {
														$value ['id'] = $sitepage->getIdentity ();
														$location = Engine_Api::_ ()->getDbtable ( 'locations', 'sitepage' )->getLocation ( $value );
														if (! empty ( $location )) {
															$flag = true;
														}
													}
												}
												break;
											case 'socialengineaddon.feed' :
												$flag = true;
												break;
											case 'activity.feed' :
												$flag = true;
												break;
											case 'sitepage.info-sitepage' :
												$flag = true;
												break;
											case 'core.profile-links' :
												$flag = true;
												break;
											case 'sitepagenote.profile-sitepagenotes' :
												if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
													if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagenote" ) == 1) {
														$flag = true;
													}
												} else {
													$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'sncreate' );
													if (! empty ( $isPageOwnerAllow )) {
														$flag = true;
													}
												}
												break;
											case 'sitepageevent.profile-sitepageevents' :
												if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
													if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepageevent" ) == 1) {
														$flag = true;
													}
												} else {
													$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'secreate' );
													if (! empty ( $isPageOwnerAllow )) {
														$flag = true;
													}
												}
												break;
											case 'sitepage.discussion-sitepage' :
												if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
													if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagediscussion" ) == 1) {
														$flag = true;
													}
												} else {
													$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'comment' );
													if (! empty ( $isPageOwnerAllow )) {
														$flag = true;
													}
												}
												break;
											case 'sitepage.photos-sitepage' :
												if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
													if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagealbum" ) == 1) {
														$flag = true;
													}
												} else {
													$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'spcreate' );
													if (! empty ( $isPageOwnerAllow )) {
														$flag = true;
													}
												}
												break;
											case 'sitepagemusic.profile-sitepagemusic' :
												if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
													if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagemusic" ) == 1) {
														$flag = true;
													}
												} else {
													$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'smcreate' );
													if (! empty ( $isPageOwnerAllow )) {
														$flag = true;
													}
												}
												break;
											case 'sitepagedocument.profile-sitepagedocuments' :
												if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
													if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagedocument" ) == 1) {
														$flag = true;
													}
												} else {
													$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'sdcreate' );
													if (! empty ( $isPageOwnerAllow )) {
														$flag = true;
													}
												}
												break;
											case 'sitepagereview.profile-sitepagereviews' :
												$flag = true;
												break;
											case 'sitepagevideo.profile-sitepagevideos' :
												if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
													if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagevideo" ) == 1) {
														$flag = true;
													}
												} else {
													$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'svcreate' );
													if (! empty ( $isPageOwnerAllow )) {
														$flag = true;
													}
												}
												break;
											case 'sitepagepoll.profile-sitepagepolls' :
												if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
													if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepagepoll" ) == 1) {
														$flag = true;
													}
												} else {
													$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'splcreate' );
													if (! empty ( $isPageOwnerAllow )) {
														$flag = true;
													}
												}
												break;
											case 'sitepageoffer.profile-sitepageoffers' :
												if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
													if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepageoffer" ) == 1) {
														$flag = true;
													}
												} else {
													$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'offer' );
													if (! empty ( $isPageOwnerAllow )) {
														$flag = true;
													}
												}
												break;
											case 'sitepageform.sitepage-viewform' :
												if (Engine_Api::_ ()->sitepage ()->hasPackageEnable ()) {
													if (Engine_Api::_ ()->sitepage ()->allowPackageContent ( $sitepage->package_id, "modules", "sitepageform" ) == 1) {
														$flag = true;
													}
												} else {
													$isPageOwnerAllow = Engine_Api::_ ()->sitepage ()->isPageOwnerAllow ( $sitepage, 'form' );
													if (! empty ( $isPageOwnerAllow )) {
														$flag = true;
													}
												}
												break;
										}
										if (! empty ( $flag )) {
											$content_ids = $value ['content_id'];
											$content_names = $value ['name'];
											break;
										}
									}
								}
								
								$params = array ('content_id' => $content_ids, 'content_name' => $content_names );
								$tempcontent_name = $params ['content_name'];
								$tempcontent_id = $params ['content_id'];
								$newtabid = Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'tab', $tempcontent_id );
								$tab_main = Zend_Controller_Front::getInstance ()->getRequest ()->getParam ( 'tab', null );
								if ($tab_main) {
									$current_tab_name = $tableCore->select ()->from ( $tableCore->info ( 'name' ), array ('name' ) )->where ( 'content_id = ?', $tab_main )->query ()->fetchColumn ();
									$tempcontent_name = $current_tab_name;
									$tempcontent_id = $tab_main;
								}
							}
						}
					}
					
					if (Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'communityad' ) && ! empty ( $contentinfo )) {
						$page_communityads = Engine_Api::_ ()->getApi ( 'settings', 'core' )->getSetting ( 'sitepage.communityads', 1 );
					} else {
						$page_communityads = 0;
					}
					
					$siteinfo = $view->layout ()->siteinfo;
					if (! empty ( $sitepage )) {
						if ($sitepage->category_id) {
							$row = Engine_Api::_ ()->getDbTable ( 'categories', 'sitepage' )->getCategory ( $sitepage->category_id );
							if (! empty ( $row->category_name )) {
								$category_name = $row->category_name;
								$siteinfo ['keywords'] = $category_name;
							}
							$row = Engine_Api::_ ()->getDbTable ( 'categories', 'sitepage' )->getCategory ( $sitepage->subcategory_id );
							if (! empty ( $row->category_name )) {
								$subcategory_name = $row->category_name;
								$siteinfo ['keywords'] .= ',' . $subcategory_name;
							}
						}
						if (! empty ( $sitepage->location )) {
							$siteinfo ['keywords'] .= ',' . $sitepage->location;
						}
					}
					
					$view->layout ()->siteinfo = $siteinfo;
					$overview = $view->translate ( 'Overview' );
					$form = $view->translate ( 'Form' );
					$review = $view->translate ( 'Reviews' );
					$document = $view->translate ( 'Documents' );
					$offer = $view->translate ( 'Offers' );
					$poll = $view->translate ( 'Polls' );
					$event = $view->translate ( 'Events' );
					$note = $view->translate ( 'Notes' );
					$photo = $view->translate ( 'Photos' );
					$discussion = $view->translate ( 'Discussions' );
					$map = $view->translate ( 'Map' );
					$link = $view->translate ( 'Links' );
					$video = $view->translate ( 'Videos' );
					$music = $view->translate ( 'Music' );
					
					$script = <<<EOF

	    var page_communityads = '$page_communityads';
	    var contentinformation = '$contentinformation';
	    var page_showtitle = 0;
	    var prev_tab_class = '';
	    if(contentinformation == 0) {
	      page_showtitle = 1;
	    }
      window.addEvent('domready', function() {

	    	  if($('main_tabs')) {

					switch ("$tempcontent_name") {
            case 'sitepage.photos-sitepage':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Photo, '$tempcontent_id', 'photo', 'sitepage', 'photos-sitepage', page_showtitle, 'null', photo_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepage_photos_sitepage')) {
									hideLeftContainer (photo_ads_display, page_communityad_integration, adwithoutpackage);
							  }
							  if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
								page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepage_photos_sitepage')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepage_photos_sitepage'));
                }
							}
            break;
            case 'sitepagevideo.profile-sitepagevideos':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Video, '$tempcontent_id', 'video', 'sitepagevideo', 'profile-sitepagevideos', page_showtitle, 'null', video_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagevideo_profile_sitepagevideos')) {
									hideLeftContainer (video_ads_display, page_communityad_integration, adwithoutpackage);
							  }
							  if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
								page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepagevideo_profile_sitepagevideos')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepagevideo_profile_sitepagevideos'));
                }
							}
            break;
            case 'sitepagenote.profile-sitepagenotes':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Note, '$tempcontent_id', 'note', 'sitepagenote', 'profile-sitepagenotes', page_showtitle, 'null', note_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagenote_profile_sitepagenotes')) {
									hideLeftContainer (note_ads_display, page_communityad_integration, adwithoutpackage);
							  }
							  if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
								page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepagenote_profile_sitepagenotes')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepagenote_profile_sitepagenotes'));
                }
							}
            break;
            case 'sitepagereview.profile-sitepagereviews':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Review, '$tempcontent_id', 'review', 'sitepagereview', 'profile-sitepagereviews', page_showtitle,'null', review_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagereview_profile_sitepagereviews')) {
									hideLeftContainer (review_ads_display, page_communityad_integration, adwithoutpackage);
							  }
							  if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
								page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepagereview_profile_sitepagereviews')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepagereview_profile_sitepagereviews'));
                }
							}
            break;
            case 'sitepageform.sitepage-viewform':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Form, '$tempcontent_id', 'form', 'sitepageform', 'sitepage-viewform', page_showtitle, '$page_url', form_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepageform_sitepage_viewform')) {
									hideLeftContainer (form_ads_display, page_communityad_integration, adwithoutpackage);
							  }
							  if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
								page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepageform_sitepage_viewform')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepageform_sitepage_viewform'));
                }
							}
            break;
            case 'sitepagedocument.profile-sitepagedocuments':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Document, '$tempcontent_id', 'document', 'sitepagedocument', 'profile-sitepagedocuments', page_showtitle, 'null', document_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagedocument_profile_sitepagedocuments')) {
									hideLeftContainer (document_ads_display, page_communityad_integration, adwithoutpackage);
							  }
							  if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
								page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepagedocument_profile_sitepagedocuments')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepagedocument_profile_sitepagedocuments'));
                }
							}
            break;
            case 'sitepageevent.profile-sitepageevents':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Event, '$tempcontent_id', 'event', 'sitepageevent', 'profile-sitepageevents', page_showtitle,'null', event_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepageevent_profile_sitepageevents')) {
									hideLeftContainer (event_ads_display, page_communityad_integration, adwithoutpackage);
							  }
							  if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
								page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepageevent_profile_sitepageevents')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepageevent_profile_sitepageevents'));
                }
							}
            break;
            case 'sitepagepoll.profile-sitepagepolls':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Poll, '$tempcontent_id', 'poll', 'sitepagepoll', 'profile-sitepagepolls', page_showtitle,'null', poll_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagepoll_profile_sitepagepolls')) {
									hideLeftContainer (poll_ads_display, page_communityad_integration, adwithoutpackage);
							  }
							  if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
								page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepagepoll_profile_sitepagepolls')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepagepoll_profile_sitepagepolls'));
                }
							}
            break;
            case 'sitepagemusic.profile-sitepagemusic':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Music, '$tempcontent_id', 'music', 'sitepagemusic', 'profile-sitepagemusic', page_showtitle,'null', music_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagemusic_profile_sitepagemusic')) {
									hideLeftContainer (music_ads_display, page_communityad_integration, adwithoutpackage);
							  }
							  if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
								page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepagemusic_profile_sitepagemusic')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepagemusic_profile_sitepagemusic'));
                }
							}
            break;
            case 'sitepageoffer.profile-sitepageoffers':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Offer, '$tempcontent_id', 'offer', 'sitepageoffer', 'profile-sitepageoffers', page_showtitle,'null', offer_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepageoffer_profile_sitepageoffers')) {
									hideLeftContainer (offer_ads_display, page_communityad_integration, adwithoutpackage);
							  }
							  if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
								page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepageoffer_profile_sitepageoffers')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepageoffer_profile_sitepageoffers'));
                }
							}
            break;
            case 'sitepage.discussion-sitepage':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Discusssion, '$tempcontent_id', 'discussion', 'sitepage', 'discussion-sitepage', page_showtitle, 'null', discussion_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepage_discussion_sitepage')) {
									hideLeftContainer (discussion_ads_display, page_communityad_integration, adwithoutpackage);
							  }
							  if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
                page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepage_discussion_sitepage')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepage_discussion_sitepage'));
                }
							}
            break;
            case 'sitepage.overview-sitepage':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                hideLeftContainer (overview_ads_display, page_communityad_integration, adwithoutpackage);
                prev_tab_id = "$newtabid";
                prev_tab_class = 'layout_sitepage_overview_sitepage';
						    if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
                page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepage_overview_sitepage')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepage_overview_sitepage'));
                }
							}
            break;
            case 'core.profile-links':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
						    if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
                if($('main_tabs').getElement('.tab_layout_core_profile_links')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_core_profile_links'));
                }
								page_showtitle = 0;
							}
            break;
            case 'sitepage.location-sitepage':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
      					hideLeftContainer (location_ads_display, page_communityad_integration, adwithoutpackage);
						    if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
                if($('main_tabs').getElement('.tab_layout_sitepage_location_sitepage')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepage_location_sitepage'));
                }
								page_showtitle = 0;
							}
            break;
            case 'sitepage.info-sitepage':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
								page_showtitle = 0;
                if($('main_tabs').getElement('.tab_layout_sitepage_info_sitepage')) {
                  tabContainerSwitch($('main_tabs').getElement('.tab_layout_sitepage_info_sitepage'));
                }
							}
            break;
            case 'activity.feed':
              tabContainerSwitch($('main_tabs').getElement('.tab_layout_activity_feed'));
              break;
            case 'socialengineaddon.feed':
              tabContainerSwitch($('main_tabs').getElement('.tab_layout_socialengineaddon_feed'));
              break;
					 }

			    if($('main_tabs').getElement('.tab_$tab_id')){
			      $('main_tabs').getElement('.tab_$tab_id').addEvent('click', function() {
			      if($('profile_status')) {
			        $('profile_status').innerHTML = "<h2>$sitepage_title</h2>";
            }
            if($('main_tabs').getElement('.tab_layout_activity_feed')) {
              tabContainerSwitch($('main_tabs').getElement('.tab_layout_activity_feed'));
            }
						if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
               $('global_content').getElement('.layout_sitepage_photorecent_sitepage').innerHTML=layout_sitepage_photorecent;
							 $('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'block';
							 if($('photo_image_recent')) {
	               $('photo_image_recent').style.display = 'block';
							 }
               if($('white_content_default'))
                  $('white_content_default').addEvent('click', function(event) {
                  event.stopPropagation();
               });
						}
			      prev_tab_id = '$tab_id';
					  if ($$('.layout_left')){
					    $$('.layout_left').setStyle('display', 'block');
					    if($('thumb_icon')) {
                $('thumb_icon').style.display = 'none';
              }
					  }
					});
				  }
				}
				else
	      {
	       switch ("$tempcontent_name") {
            case 'sitepage.photos-sitepage':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $photo </h2>";
								}
								$('global_content').getElement('.layout_sitepage_photos_sitepage > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $photo</div>";
		            ShowContent('$tempcontent_id', execute_Request_Photo, '$tempcontent_id', 'photo', 'sitepage', 'photos-sitepage', page_showtitle, 'null', photo_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepage_photos_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photos_sitepage').style.display = 'block';
									prev_tab_id = "$newtabid";
									prev_tab_class = 'layout_sitepage_photos_sitepage';
							  }
							  hideWidgets();
							}
            break;
            case 'sitepagevideo.profile-sitepagevideos':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $video </h2>";
								}
								$('global_content').getElement('.layout_sitepagevideo_profile_sitepagevideos > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $video</div>";
		            ShowContent('$tempcontent_id', execute_Request_Video, '$tempcontent_id', 'video', 'sitepagevideo', 'profile-sitepagevideos', page_showtitle, 'null', video_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagevideo_profile_sitepagevideos')) {
									$('global_content').getElement('.layout_sitepagevideo_profile_sitepagevideos').style.display = 'block';
									prev_tab_id = "$newtabid";
									prev_tab_class = 'layout_sitepagevideo_profile_sitepagevideos';
							  }
								hideWidgets();
							}
            break;
            case 'sitepagenote.profile-sitepagenotes':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $note </h2>";
								}
								$('global_content').getElement('.layout_sitepagenote_profile_sitepagenotes > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $note</div>";
		            ShowContent('$tempcontent_id', execute_Request_Note, '$tempcontent_id', 'note', 'sitepagenote', 'profile-sitepagenotes', page_showtitle, 'null', note_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagenote_profile_sitepagenotes')) {
									$('global_content').getElement('.layout_sitepagenote_profile_sitepagenotes').style.display = 'block';
									prev_tab_id = "$newtabid";
									prev_tab_class = 'layout_sitepagenote_profile_sitepagenotes';
							  }
								hideWidgets();
							}
            break;
            case 'sitepagereview.profile-sitepagereviews':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $review </h2>";
								}
								$('global_content').getElement('.layout_sitepagereview_profile_sitepagereviews > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $review </div>";
		            ShowContent('$tempcontent_id', execute_Request_Review, '$tempcontent_id', 'review', 'sitepagereview', 'profile-sitepagereviews', page_showtitle,'null', review_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagereview_profile_sitepagereviews')) {
									$('global_content').getElement('.layout_sitepagereview_profile_sitepagereviews').style.display = 'block';
									prev_tab_id = "$newtabid";
									prev_tab_class = 'layout_sitepagereview_profile_sitepagereviews';
							  }
								hideWidgets();
							}
            break;
            case 'sitepageform.sitepage-viewform':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $form </h2>";
								}
								$('global_content').getElement('.layout_sitepageform_sitepage_viewform > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $form </div>";
		            ShowContent('$tempcontent_id', execute_Request_Form, '$tempcontent_id', 'form', 'sitepageform', 'sitepage-viewform', page_showtitle, '$page_url', form_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepageform_sitepage_viewform')) {
									$('global_content').getElement('.layout_sitepageform_sitepage_viewform').style.display = 'block';
									prev_tab_id = "$newtabid";
									prev_tab_class = 'layout_sitepageform_sitepage_viewform';
							  }
								hideWidgets();
							}
            break;
            case 'sitepagedocument.profile-sitepagedocuments':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $document </h2>";
								}
								$('global_content').getElement('.layout_sitepagedocument_profile_sitepagedocuments > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $document </div>";
		            ShowContent('$tempcontent_id', execute_Request_Document, '$tempcontent_id', 'document', 'sitepagedocument', 'profile-sitepagedocuments', page_showtitle, 'null', document_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagedocument_profile_sitepagedocuments')) {
									$('global_content').getElement('.layout_sitepagedocument_profile_sitepagedocuments').style.display = 'block';
									prev_tab_id = "$newtabid";
									prev_tab_class = 'layout_sitepagedocument_profile_sitepagedocuments';
							  }
								hideWidgets();
							}
            break;
            case 'sitepageevent.profile-sitepageevents':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $event </h2>";
								}
								$('global_content').getElement('.layout_sitepageevent_profile_sitepageevents > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $event </div>";
		            ShowContent('$tempcontent_id', execute_Request_Event, '$tempcontent_id', 'event', 'sitepageevent', 'profile-sitepageevents', page_showtitle,'null', event_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepageevent_profile_sitepageevents')) {
									$('global_content').getElement('.layout_sitepageevent_profile_sitepageevents').style.display = 'block';
									prev_tab_id = "$newtabid";
									prev_tab_class = 'layout_sitepageevent_profile_sitepageevents';
							  }
								hideWidgets();
							}
            break;
            case 'sitepagepoll.profile-sitepagepolls':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $poll </h2>";
								}
								$('global_content').getElement('.layout_sitepagepoll_profile_sitepagepolls > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $poll </div>";
		            ShowContent('$tempcontent_id', execute_Request_Poll, '$tempcontent_id', 'poll', 'sitepagepoll', 'profile-sitepagepolls', page_showtitle,'null', poll_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagepoll_profile_sitepagepolls')) {
									$('global_content').getElement('.layout_sitepagepoll_profile_sitepagepolls').style.display = 'block';
									prev_tab_id = "$newtabid";
									prev_tab_class = 'layout_sitepagepoll_profile_sitepagepolls';
							  }
								hideWidgets();
							}
            break;
            case 'sitepagemusic.profile-sitepagemusic':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $music </h2>";
								}
								$('global_content').getElement('.layout_sitepagemusic_profile_sitepagemusic > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $music </div>";
		            ShowContent('$tempcontent_id', execute_Request_Music, '$tempcontent_id', 'music', 'sitepagemusic', 'profile-sitepagemusic', page_showtitle,'null', music_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepagemusic_profile_sitepagemusic')) {
									$('global_content').getElement('.layout_sitepagemusic_profile_sitepagemusic').style.display = 'block';
									prev_tab_id = "$newtabid";
									prev_tab_class = 'layout_sitepagemusic_profile_sitepagemusic';
							  }
								hideWidgets();
							}
            break;
            case 'sitepageoffer.profile-sitepageoffers':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $offer </h2>";
								}
								$('global_content').getElement('.layout_sitepageoffer_profile_sitepageoffers > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $offer </div>";
		            ShowContent('$tempcontent_id', execute_Request_Offer, '$tempcontent_id', 'offer', 'sitepageoffer', 'profile-sitepageoffers', page_showtitle,'null', offer_ads_display, page_communityad_integration);
						  	if($('global_content').getElement('.layout_sitepageoffer_profile_sitepageoffers')) {
									$('global_content').getElement('.layout_sitepageoffer_profile_sitepageoffers').style.display = 'block';
									prev_tab_id = "$newtabid";
									prev_tab_class = 'layout_sitepageoffer_profile_sitepageoffers';
							  }
								hideWidgets();
							}
            break;
            case 'sitepage.discussion-sitepage':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
		            ShowContent('$tempcontent_id', execute_Request_Discusssion, '$tempcontent_id', 'discussion', 'sitepage', 'discussion-sitepage', page_showtitle, 'null', discussion_ads_display, page_communityad_integration);
		            if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $discussion </h2>";
								}
								$('global_content').getElement('.layout_sitepage_discussion_sitepage > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $discussion </div>";
						  	if($('global_content').getElement('.layout_sitepage_discussion_sitepage')) {
									$('global_content').getElement('.layout_sitepage_discussion_sitepage').style.display = 'block';
									prev_tab_id = "$newtabid";
									prev_tab_class = 'layout_sitepage_discussion_sitepage';
							  }
								hideWidgets();
							}
            break;
            case 'sitepage.overview-sitepage':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
						    if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
									$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
								}
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $overview </h2>";
								}
								$('global_content').getElement('.layout_sitepage_overview_sitepage > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $overview</div>";

						    if($('global_content').getElement('.layout_sitepage_overview_sitepage')) {
									 $('global_content').getElement('.layout_sitepage_overview_sitepage').style.display = 'block';
									 prev_tab_id = "$newtabid";
									 prev_tab_class = 'layout_sitepage_overview_sitepage';
							  }
								hideWidgetsForModule('sitepageoverview');
							}
            break;
            case 'core.profile-links':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $link </h2>";
								}
								$('global_content').getElement('.layout_core_profile_links > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's  $link</div>";
                hideWidgetsForModule('sitepagelink');
							}
            break;
            case 'sitepage.location-sitepage':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                if($('profile_status')) {
		    			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $map </h2>";
								}
								$('global_content').getElement('.layout_sitepage_location_sitepage > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's $map</div>";
						    if($('global_content').getElement('.layout_sitepage_location_sitepage')) {
									 $('global_content').getElement('.layout_sitepage_location_sitepage').style.display = 'block';
									 prev_tab_id = "$newtabid";
									 prev_tab_class = 'layout_sitepage_location_sitepage';
							  }
								hideWidgetsForModule('sitepagelocation');
							}
            break;
            case 'activity.feed':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
						    hideWidgetsForModule('sitepageactivityfeed');
                if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
                  $('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'block';
                }
							}
            break;
           case 'socialengineaddon.feed':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
                hideWidgetsForModule('sitepagesocialengineaddonactivityfeed');
						    if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
                  $('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'block';
                }
						  }
            break;
            case 'sitepage.info-sitepage':
              if($newtabid == "$tempcontent_id" && $newtabid != 0) {
						    hideWidgetsForModule('sitepageinfo');
                if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
                  $('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'block';
                }
							}
            break;
					}
				  if($widgetinformation == 0) {
				    if($('global_content').getElement('.layout_sitepage_location_sitepage')) {
							$('global_content').getElement('.layout_sitepage_location_sitepage').style.display = 'block';
						}
						if($('global_content').getElement('.layout_sitepage_info_sitepage')) {
							$('global_content').getElement('.layout_sitepage_info_sitepage').style.display = 'block';
						}
						if($('global_content').getElement('.layout_sitepage_info_sitepage')) {
							$('global_content').getElement('.layout_sitepage_info_sitepage').style.display = 'block';
						}
						if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
               $('global_content').getElement('.layout_sitepage_photorecent_sitepage').innerHTML=layout_sitepage_photorecent;
							$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'block';
	         			if($('photo_image_recent')) {
					         $('photo_image_recent').style.display = 'block';
								}
                if($('white_content_default'))
                  $('white_content_default').addEvent('click', function(event) {
                  event.stopPropagation();
                  });
						}

						if($('global_content').getElement('.layout_core_profile_links')) {
							$('global_content').getElement('.layout_core_profile_links').style.display = 'block';
						}
					}

    			$$('.tab_$tab_id').addEvent('click', function() {
    			  if($('profile_status')) {
    			    $('profile_status').innerHTML = "<h2>$sitepage_title</h2>";
						}
						if($('global_content').getElement('.layout_sitepage_location_sitepage')) {
							$('global_content').getElement('.layout_sitepage_location_sitepage').style.display = 'none';
						}
            if($('global_content').getElement('.layout_sitepage_photos_sitepage')) {
					    $('global_content').getElement('.layout_sitepage_photos_sitepage').style.display = 'none';
            }
            if($('global_content').getElement('.layout_sitepagevideo_profile_sitepagevideos')) {
					    $('global_content').getElement('.layout_sitepagevideo_profile_sitepagevideos').style.display = 'none';
            }
            if($('global_content').getElement('.layout_sitepage_discussion_sitepage')) {
					    $('global_content').getElement('.layout_sitepage_discussion_sitepage').style.display = 'none';
            }
            if($('global_content').getElement('.layout_sitepageoffer_profile_sitepageoffers')) {
					    $('global_content').getElement('.layout_sitepageoffer_profile_sitepageoffers').style.display = 'none';
            }
            if($('global_content').getElement('.layout_sitepagedocument_profile_sitepagedocuments')) {
					    $('global_content').getElement('.layout_sitepagedocument_profile_sitepagedocuments').style.display = 'none';
            }
            if($('global_content').getElement('.layout_sitepagereview_profile_sitepagereviews')) {
					    $('global_content').getElement('.layout_sitepagereview_profile_sitepagereviews').style.display = 'none';
            }
            if($('global_content').getElement('.layout_sitepagepoll_profile_sitepagepolls')) {
					    $('global_content').getElement('.layout_sitepagepoll_profile_sitepagepolls').style.display = 'none';
            }
            if($('global_content').getElement('.layout_sitepagenote_profile_sitepagenotes')) {
					    $('global_content').getElement('.layout_sitepagenote_profile_sitepagenotes').style.display = 'none';
            }
            if($('global_content').getElement('.layout_sitepageevent_profile_sitepageevents')) {
					    $('global_content').getElement('.layout_sitepageevent_profile_sitepageevents').style.display = 'none';
            }
            if($('global_content').getElement('.layout_sitepagemusic_profile_sitepagemusic')) {
					    $('global_content').getElement('.layout_sitepagemusic_profile_sitepagemusic').style.display = 'none';
            }
      			if($('global_content').getElement('.layout_sitepageform_sitepage_viewform')) {
						  $('global_content').getElement('.layout_sitepageform_sitepage_viewform').style.display = 'none';
            }
						if($('global_content').getElement('.layout_core_profile_links')) {
							$('global_content').getElement('.layout_core_profile_links').style.display = 'none';
						}
						if($('global_content').getElement('.layout_sitepage_info_sitepage')) {
							$('global_content').getElement('.layout_sitepage_info_sitepage').style.display = 'none';
						}
						if($('global_content').getElement('.layout_sitepage_overview_sitepage')) {
							$('global_content').getElement('.layout_sitepage_overview_sitepage').style.display = 'none';
					  }
						if($('global_content').getElement('.layout_activity_feed')) {
							$('global_content').getElement('.layout_activity_feed').style.display = 'block';
						}
        	  if($('global_content').getElement('.layout_socialengineaddon_feed')) {
							$('global_content').getElement('.layout_socialengineaddon_feed').style.display = 'block';
						}
				    if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
               $('global_content').getElement('.layout_sitepage_photorecent_sitepage').innerHTML=layout_sitepage_photorecent;
               $('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'block';
         			 if($('photo_image_recent')) {
				         $('photo_image_recent').style.display = 'block';
							 }
               if($('white_content_default'))
                $('white_content_default').addEvent('click', function(event) {
                event.stopPropagation();
                });
						}
				    if ($('id_' + prev_tab_id) != null && prev_tab_id != 0 && prev_tab_id != '$tab_id') {
				      $('id_' + prev_tab_id).style.display = "none";
				    }
			    prev_tab_id = '$tab_id';
				 });

  			$$('.tab_$tab_id3').addEvent('click', function() {
  			  if($('profile_status')) {
  			    $('profile_status').innerHTML = "<h2>$sitepage_title &raquo; $link </h2>";
					}
					$('global_content').getElement('.layout_core_profile_links > h3').innerHTML = "<div class='layout_simple_head'>$sitepage_title's $link</div>";

					if($('global_content').getElement('.layout_sitepage_location_sitepage')) {
						$('global_content').getElement('.layout_sitepage_location_sitepage').style.display = 'none';
					}
					if($('global_content').getElement('.layout_core_profile_links')) {
						$('global_content').getElement('.layout_core_profile_links').style.display = 'block';
					}
					if($('global_content').getElement('.layout_sitepage_info_sitepage')) {
						$('global_content').getElement('.layout_sitepage_info_sitepage').style.display = 'none';
					}
			    if($('global_content').getElement('.layout_sitepage_photorecent_sitepage')) {
						$('global_content').getElement('.layout_sitepage_photorecent_sitepage').style.display = 'none';
					}
					if($('global_content').getElement('.layout_activity_feed')) {
						$('global_content').getElement('.layout_activity_feed').style.display = 'none';
					}
        	if($('global_content').getElement('.layout_socialengineaddon_feed')) {
						$('global_content').getElement('.layout_socialengineaddon_feed').style.display = 'none';
					}
					if($('global_content').getElement('.layout_sitepage_overview_sitepage')) {
						$('global_content').getElement('.layout_sitepage_overview_sitepage').style.display = 'none';
				  }
			    if ($('id_' + prev_tab_id) != null && prev_tab_id != 0 && prev_tab_id != '$tab_id3') {
			      $('id_' + prev_tab_id).style.display = "none";
			    }
		    prev_tab_id = '$tab_id3';
		 	});
     }
	 });
	 window.addEvent('domready', function() {

      if($('thumb_icon')) {
	      if($currenttabid == 0) {
	       $('thumb_icon').style.display = 'none';
			  }
	    }
		});
EOF;
					
					if ("$tempcontent_name" == 'sitepage.discussion-sitepage' || "$tempcontent_name" == 'sitepage.photos-sitepage' || "$tempcontent_name" == 'sitepagevideo.profile-sitepagevideos' || "$tempcontent_name" == 'sitepagenote.profile-sitepagenotes' || "$tempcontent_name" == 'sitepagereview.profile-sitepagereviews' || "$tempcontent_name" == 'sitepageform.sitepage-viewform' || "$tempcontent_name" == 'sitepagedocument.profile-sitepagedocuments' || "$tempcontent_name" == 'sitepageevent.profile-sitepageevents' || "$tempcontent_name" == 'sitepagepoll.profile-sitepagepolls' || "$tempcontent_name" == 'sitepagemusic.profile-sitepagemusic' || "$tempcontent_name" == 'sitepageoffer.profile-sitepageoffers') {
						Engine_Api::_ ()->sitepage ()->showAdWithPackage ( $sitepage );
						$view->headScript ()->appendFile ( 'application/modules/Sitepage/externals/scripts/hideWidgets.js' )->appendScript ( $script );
					}
					
					$view->headScript ()->appendFile ( 'application/modules/Sitepage/externals/scripts/hideTabs.js' )->appendScript ( $script );
					
					$view->headScript ()->appendFile ( 'application/modules/Sitepage/externals/scripts/core.js' )->appendScript ( $script );
				}
			}
		}
	}
	
	public function onStatistics($event) {
		$table = Engine_Api::_ ()->getDbTable ( 'pages', 'sitepage' );
		$select = new Zend_Db_Select ( $table->getAdapter () );
		$select->from ( $table->info ( 'name' ), 'COUNT(*) AS count' );
		$event->addResponse ( $select->query ()->fetchColumn ( 0 ), 'page' );
	}
	
	public function onUserDeleteBefore($event) {
		$payload = $event->getPayload ();
		
		if ($payload instanceof User_Model_User) {
			
			$user_id = $payload->getIdentity ();
			
			//GET PAGE TABLE
			$sitepageTable = Engine_Api::_ ()->getDbtable ( 'pages', 'sitepage' );
			
			Engine_Api::_ ()->getDbtable ( 'claims', 'sitepage' )->delete ( array ('user_id =?' => $user_id ) );
			
			Engine_Api::_ ()->getDbtable ( 'listmemberclaims', 'sitepage' )->delete ( array ('user_id = ?' => $user_id ) );
			
			Engine_Api::_ ()->getDbtable ( 'manageadmins', 'sitepage' )->delete ( array ('user_id = ?' => $user_id ) );
			
			//START ALBUM CODE
			$table = Engine_Api::_ ()->getItemTable ( 'sitepage_photo' );
			$select = $table->select ()->where ( 'user_id = ?', $user_id );
			$rows = $table->fetchAll ( $select );
			if (! empty ( $rows )) {
				foreach ( $rows as $photo ) {
					$photo->delete ();
				}
			}
			
			$table = Engine_Api::_ ()->getItemTable ( 'sitepage_album' );
			$select = $table->select ()->where ( 'owner_id = ?', $user_id );
			$rows = $table->fetchAll ( $select );
			if (! empty ( $rows )) {
				foreach ( $rows as $album ) {
					$album->delete ();
				}
			}
			//END ALBUM CODE
			

			//START DISUCSSION CODE
			$sitepageDiscussionEnabled = Engine_Api::_ ()->getDbtable ( 'modules', 'core' )->isModuleEnabled ( 'sitepagediscussion' );
			if ($sitepageDiscussionEnabled) {
				
				$table = Engine_Api::_ ()->getItemTable ( 'sitepage_topic' );
				$select = $table->select ()->where ( 'user_id = ?', $user_id );
				$rows = $table->fetchAll ( $select );
				if (! empty ( $rows )) {
					foreach ( $rows as $topic ) {
						$topic->delete ();
					}
				}
				
				$table = Engine_Api::_ ()->getItemTable ( 'sitepage_post' );
				$select = $table->select ()->where ( 'user_id = ?', $user_id );
				$rows = $table->fetchAll ( $select );
				if (! empty ( $rows )) {
					foreach ( $rows as $post ) {
						$post->delete ();
					}
				}
				
				Engine_Api::_ ()->getDbtable ( 'topicwatches', 'sitepage' )->delete ( array ('user_id = ?' => $user_id ) );
			}
			//END DISUCSSION CODE
			

			$sitepageSelect = $sitepageTable->select ()->where ( 'owner_id = ?', $user_id );
			
			foreach ( $sitepageTable->fetchAll ( $sitepageSelect ) as $sitepage ) {
				Engine_Api::_ ()->sitepage ()->onPageDelete ( $sitepage->page_id );
			}
		}
	}

}

?>